﻿using System;

namespace Test30.Engine
{
    [Flags]
    public enum ActionResultFlags
    {
        Default        = 0x0000,
        Done           = 0x0001,
        NeedsPause     = 0x0002,
        Failed         = 0x0004,
        CheckForCancel = 0x0008
    }

    public class ActionResult
    {
        public static ActionResult Done => new ActionResult(ActionResultFlags.Done);
        public static ActionResult DoneAndPause => new ActionResult(ActionResultFlags.Done | ActionResultFlags.NeedsPause);
        public static ActionResult NotDone => new ActionResult(ActionResultFlags.Default);
        public static ActionResult Fail => new ActionResult(ActionResultFlags.Failed | ActionResultFlags.Done);
        public static ActionResult CheckForCancel =>new ActionResult(ActionResultFlags.CheckForCancel | ActionResultFlags.Done);
        public bool Success => !IsFlagSet(ActionResultFlags.Failed);
        public bool NeedsPause => IsFlagSet(ActionResultFlags.NeedsPause);
        public bool IsDone => IsFlagSet(ActionResultFlags.Done);
        public bool NeedsCheckForCancel => IsFlagSet(ActionResultFlags.CheckForCancel);
        public Action Alternate { get; }

        public ActionResult(ActionResultFlags flags)
        {
            _flags = flags;
        }
        public ActionResult(Action alternate)
        {
            Alternate = alternate;
        }
        private bool IsFlagSet(ActionResultFlags flags)
        {
            return (_flags & flags) == flags;
        }
        private readonly ActionResultFlags _flags;
    }
}
